package week01;

/**
 * 设计循环双端队列
 * <p>
 * 设计实现双端队列。
 * 你的实现需要支持以下操作：
 * <p>
 * MyCircularDeque(k)：构造函数,双端队列的大小为k。
 * insertFront()：将一个元素添加到双端队列头部。 如果操作成功返回 true。
 * insertLast()：将一个元素添加到双端队列尾部。如果操作成功返回 true。
 * deleteFront()：从双端队列头部删除一个元素。 如果操作成功返回 true。
 * deleteLast()：从双端队列尾部删除一个元素。如果操作成功返回 true。
 * getFront()：从双端队列头部获得一个元素。如果双端队列为空，返回 -1。
 * getRear()：获得双端队列的最后一个元素。 如果双端队列为空，返回 -1。
 * isEmpty()：检查双端队列是否为空。
 * isFull()：检查双端队列是否满了。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/design-circular-deque
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class Code08 {

    private final int capacity;
    private int size;
    private Node head;
    private Node tail;

    /**
     * Initialize your data structure here. Set the size of the deque to be k.
     */
    public Code08(int k) {
        this.capacity = k;
    }

    /**
     * Adds an item at the front of Deque. Return true if the operation is successful.
     */
    public boolean insertFront(int value) {
        if (size >= capacity) {
            return false;
        }
        if (head == null) {
            head = new Node(value);
            tail = head;
        } else {
            Node tmp = head;
            head = new Node(value);
            head.next = tmp;
            tmp.pre = head;
        }
        size++;
        return true;
    }

    /**
     * Adds an item at the rear of Deque. Return true if the operation is successful.
     */
    public boolean insertLast(int value) {
        if (size >= capacity) {
            return false;
        }
        if (tail == null) {
            tail = new Node(value);
            head = tail;
        } else {
            Node tmp = tail;
            tail = new Node(value);
            tmp.next = tail;
            tail.pre = tmp;
        }
        size++;
        return true;
    }

    /**
     * Deletes an item from the front of Deque. Return true if the operation is successful.
     */
    public boolean deleteFront() {
        if (size <= 0) {
            return false;
        } else {
            head = head.next;
            tail = --size == 0 ? null : tail;
            if (head != null) {
                head.pre = null;
            }
            return true;
        }
    }

    /**
     * Deletes an item from the rear of Deque. Return true if the operation is successful.
     */
    public boolean deleteLast() {
        if (size <= 0) {
            return false;
        } else {
            tail = tail.pre;
            head = --size == 0 ? null : head;
            if (tail != null) {
                tail.next = null;
            }
            return true;
        }
    }

    /**
     * Get the front item from the deque.
     */
    public int getFront() {
        if (size <= 0) {
            return -1;
        } else {
            return head.val;
        }
    }

    /**
     * Get the last item from the deque.
     */
    public int getRear() {
        if (size <= 0) {
            return -1;
        } else {
            return tail.val;
        }
    }

    /**
     * Checks whether the circular deque is empty or not.
     */
    public boolean isEmpty() {
        return size <= 0;
    }

    /**
     * Checks whether the circular deque is full or not.
     */
    public boolean isFull() {
        return size == capacity;
    }

    private static class Node {
        int val;
        Node pre;
        Node next;

        public Node(int val) {
            this.val = val;
        }
    }
}
